from functools import wraps


# 在函数中定义函数


def func1(explain="func1是最外层函数"):
    print("我是func1", explain)
    print("func1开始执行")

    def func1_1():
        print("我是func1_1,位于func_1内部")

    def func1_2():
        print("我是func1_2,位于func_1内部")

    print("func1调用内部方法func1_1")
    func1_1()
    print("func1调用内部方法func1_2")
    func1_2()
    print("func1执行结束")


# 将函数作为返回值进行传递
def return_func(param: int):
    def func1_1():
        print("我是func1_1,位于func_1内部")

    def func1_2():
        print("我是func1_2,位于func_1内部")

    if param == 1:
        return func1_1()
    else:
        return func1_2()


def test_func():
    print("执行test_func")


# 将函数作为参数进行传递
def transfer_func(func):
    func
    print("执行transfer_func")


def a_func():
    print("I am func, I am executing something ......")


# 包装函数
def decorator_func(input_func):
    def wrap_the_func():
        print("do something before execute func")
        # 函数调用时需要加上()
        input_func()
        print("do something after execute func")

    # 将包装后的函数进行返回
    return wrap_the_func


# 包装函数
def a_function_decorator_func(input_func):
    def wrap_the_func():
        print("do something before execute func")
        # 函数调用时需要加上()
        # input_func()
        print("do something after execute func")

    # 将包装后的函数进行返回
    return wrap_the_func


@a_function_decorator_func
def a_function_requiring_decoration():
    print("I am a function requiring decoration")


def decorator_name(f):
    can_run = False

    @wraps(f)
    def decorated(*args, **kwargs):
        print("args", args)
        print("kwargs", kwargs)
        if not can_run:
            return "Function will not run"
        return f(*args, **kwargs)

    return decorated


@decorator_name
def my_func(*args, **kwarg):
    return "Function is running"


if __name__ == '__main__':
    # func1()
    # 外部是无法调用func1的内部函数的
    # func1_1()

    # 调用return_func返回一个函数，然后可以进行调用
    # _func = return_func(1)
    # _func

    # 将函数作为参数进行传递
    # transfer_func(test_func())

    # a_func = decorator_func(a_func)
    # a_func()

    # a_function_requiring_decoration()
    str1 = "Test000001"
    print(str1.upper().find("TEST"))
    # print(my_func([1, 2, 3], {"A": 1, "B": 2, "C": 3}))
